package com.binge.test5;

import cn.hutool.core.date.DateUtil;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextResponse;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:Main
 * Package:com.binge.test5
 * Description:
 *
 * @Author @Bin
 * @Date 2023/12/15-22:46
 * @Version: v1.0
 */
public class Main {


    public static void main(String[] args) throws AWTException {
        // 创建一个全屏透明窗口
        //JFrame frame = new JFrame();
        //frame.setAlwaysOnTop(true);
        //frame.setUndecorated(true);
        //frame.setBackground(new Color(255, 0, 0, 20));
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(width / 3, height / 3);
        //frame.setVisible(true);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入监测频率,输入为数字，单位为分钟:");
        int i = scanner.nextInt();
        //System.out.println("请输入监测关键字:");
        //String key = scanner.next();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        do {
            System.out.println("任务开始执行---当前时间:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
            //BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            //图片
            //isSpecialCharacterPresent(screenShot,key);
            robot.mouseMove(1421 , 796);
            System.out.println("鼠标移动到坐标:" + 1421 + "," + 796);
            robot.delay(500);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            try {
                TimeUnit.MINUTES.sleep(i);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } while (true);


    }

    private static boolean isSpecialCharacterPresent(BufferedImage screenShot, String key) {
        //封装request
// 将 BufferedImage 编码为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(screenShot, "png", baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 将字节数组包装到 ByteArrayInputStream 对象中
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
// 调用接口
        com.aliyun.ocr_api20210707.models.RecognizeAllTextRequest recognizeAllTextRequest = new com.aliyun.ocr_api20210707.models.RecognizeAllTextRequest()
                .setType("Advanced")
                .setOutputOricoord(true)
                .setOutputCoordinate(com.aliyun.teautil.Common.toBytes("points"))
                .setBody(inputStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            RecognizeAllTextResponse recognizeAllTextResponse = client.recognizeAllTextWithOptions(recognizeAllTextRequest, runtime);
            List<RecognizeAllTextResponseBody.RecognizeAllTextResponseBodyDataSubImages> subImages = recognizeAllTextResponse.getBody().getData().subImages;
            subImages.forEach(subImage -> {
                List<RecognizeAllTextResponseBody.RecognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetails> blockDetails = subImage.getBlockInfo().blockDetails;
                blockDetails.forEach(blockDetail -> {
                    String text = blockDetail.blockContent;
                    if (text.contains(key)) {
                        System.out.println(text);
                        //去除坐标
                        List<RecognizeAllTextResponseBody.RecognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetailsBlockPoints> blockPoints = blockDetail.getBlockPoints();
                        for (int i = 0; i < blockPoints.size(); i++) {
                            RecognizeAllTextResponseBody.RecognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetailsBlockPoints recognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetailsBlockPoints = blockPoints.get(i);
                            Integer x = recognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetailsBlockPoints.getX() + 20;
                            Integer y = recognizeAllTextResponseBodyDataSubImagesBlockInfoBlockDetailsBlockPoints.getY() + 10;
                            robot.mouseMove(x , y );
                            System.out.println("鼠标移动到坐标:" + x + "," + y);
                            robot.delay(500);
                            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                            break;
                        }
                    }
                });
            });


        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }

        return false;
    }

    private static final Robot robot;

    private static final Client client;

    static {
        try {
            robot = new Robot();
            Config config = new Config()
                    // 必填，您的 AccessKey ID
                    .setAccessKeyId("")
                    // 必填，您的 AccessKey Secret
                    .setAccessKeySecret("");
            // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
            config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
            client = new Client(config);
        } catch (AWTException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}

